package com.agileboot.common.utils.http;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class HttpTemplate {

    public static String httpGet(String url) {
        RestTemplate restTemplate=new RestTemplate();
        String result=restTemplate.exchange(url, HttpMethod.GET,null,String.class).getBody();
        return result;
    }

    /**
     * post请求 json格式
     * @param url
     * @param parameter
     * @return
     */
    public static String httpPost(String url, Map<String, Object> parameter){
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        //转json格式
        String jsonString = JSONObject.toJSONString(parameter);
        String result = HttpUtil.createPost(url).contentType("application/json").body(jsonString).execute().body();
        return result;
    }
}
